package com.neo.rabbitmq.three;

import com.neo.rabbitmq.utils.RabbitMqUtil;
import com.neo.rabbitmq.utils.SleepUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @ClassName: Work02
 * @Title: rabbitmq-demo
 * @Package: com.neo.rabbitmq.three
 * @Description:
 * @Author: Kisen
 * @Date: 2021/9/5 23:43
 */
public class Work02 {

    // 队列名称
    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtil.getChannel();
        System.out.println("C1等待接收消息等待时间较短");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            // 沉睡1S
            SleepUtil.sleep(1);
            System.out.println("接收到的消息：" + new String(message.getBody(), "UTF-8"));
            // 手动应答
            /**
             * 1.消息的标记 tag
             * 2.是否批量应答 false：不批量应答信道中的消息 true：批量
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };
        // 设置不公平分发
        int prefetchCount = 2; // 预取值是2
        channel.basicQos(prefetchCount);
        // 采用手动应答
        boolean autoAck = false;
        channel.basicConsume(TASK_QUEUE_NAME, autoAck, deliverCallback, consumerTag -> System.out.println(consumerTag + "消费者取消消费接口回调逻辑"));
    }
}
